"""
Test cases for search API endpoints
Test cases for search API endpoints
"""

import pytest
from httpx import AsyncClient, ASGITransport, ASGITransport
from unittest.mock import patch, AsyncMock
import sys
import os

# Set test environment variables
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

os.environ["SECRET_KEY"] = "test-secret-key-for-testing-only"
os.environ["ENVIRONMENT"] = "testing"
os.environ["DEBUG"] = "true"

from main import app


@pytest.fixture
def sample_search_query():
    """Sample search query for testing"""
    return {
        "query": "test metric",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True
    }


@pytest.fixture
def auth_headers():
    """Mock authentication headers"""
    return {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0LXVzZXItMTIzIiwiZXhwIjoxNzYzNzk1ODc3fQ.g4XlulYtb4HFTX0tR2VaMZfANv0zj-WQyQ9PBkUwtwg"
    }


@pytest.mark.asyncio
async def test_search_empty_query():
    """Test search with empty query"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        search_data = {
            "query": "",
            "filters": {},
            "include_ukm": True,
            "include_uma": True,
            "include_enums": True
        }

        response = await client.post("/api/v1/search/", json=search_data)

        assert response.status_code == 422  # Validation error for empty query


@pytest.mark.asyncio
async def test_search_response_structure():
    """Test search endpoint response structure"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        search_data = {
            "query": "test",
            "filters": {},
            "include_ukm": True,
            "include_uma": True,
            "include_enums": True
        }

        response = await client.post("/api/v1/search/", json=search_data)

        # Should get 200, 401, 422, or 500 (server error during testing)
        assert response.status_code in [200, 401, 422, 500]


@pytest.mark.asyncio
async def test_search_empty_query(auth_headers):
    """Test search with empty query"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        search_data = {
            "query": "",
            "filters": {},
            "include_ukm": True,
            "include_uma": True,
            "include_enums": True
        }

        response = await client.post("/api/v1/search/", json=search_data, headers=auth_headers)

        assert response.status_code == 422  # Validation error for empty query


@pytest.mark.asyncio
async def test_get_suggestions():
    """Test getting search suggestions endpoint"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"q": "test", "limit": 10}
        response = await client.get("/api/v1/search/suggestions", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 401, 422, 500]


@pytest.mark.asyncio
async def test_get_components():
    """Test getting components list endpoint"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"limit": 20}
        response = await client.get("/api/v1/search/components", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 401, 500]


@pytest.mark.asyncio
async def test_get_tokens():
    """Test getting tokens list endpoint"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"limit": 20}
        response = await client.get("/api/v1/search/tokens", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 401, 500]